<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>模块增加页面</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <link rel="stylesheet" href="/res/layui/css/layui.css" media="all">
    <script src="/res/js/jquery.js"></script>

</head>
<body>
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>用户角色信息维护</legend>
</fieldset>
<form class="layui-form" action="" lay-filter="example">

    <input type="hidden" name="suserRoleId" id="suserRoleId"><!-- 加一个隐藏主键 -->
    <div class="layui-form-item">
        <label class="layui-form-label">角色名称</label>
        <div class="layui-input-inline">
            <select name="jue" id="jue">
                <option value="">请选择角色</option>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">姓名</label>
            <div class="layui-input-inline">
                <select name="quiz" id="quiz">
                    <option value="">请选择</option>
                    <optgroup label="客户" id="client">
                    </optgroup>
                    <optgroup label="员工" id="staff">
                    </optgroup>
                </select>
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="button" class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
            <button type="button" class="layui-btn layui-btn-primary" id="close_btn">关闭</button>
        </div>
    </div>
</form>

<script src="/res/layui/layui.js"></script>


<script>

    layui.use(['form', 'layedit', 'laydate', 'jquery'], function () {


        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate
            , $ = layui.$;

        let url = '/logisticsUserRole/selectShowStaffAndClient';
        $.post(url, "", function (resp) {
            if (resp.state == 1) {
                $.each(resp.data.data2, function (i, val) {
                    $("#staff").append("<option value="+val.staffId+",1.865ps"+val.staffName+">"+val.staffName+"</option>");

                });
                $.each(resp.data.data1, function (i, val) {
                    $("#client").append("<option value="+val.clientId+",1.865ps"+val.clientName+">"+val.clientName+"</option>");

                });

                form.render();
            }
        }, "json");

        $.post("/logisticsUserRole/selectShowJue","",function (resp){
            if (resp.state == 1) {
                $.each(resp.data, function (i, val) {
                    $("#jue").append("<option value=" + val.roleId + "> " + val.roleName + "</option>");

                });

                /**
                 * 当点击编辑按钮时会传入一个id到form。html页面
                 */
                let id = getQueryString("id");
                if (id) {
                    let url = "/logisticsUserRole/show";
                    let data_ = {id: id};
                    $.post(url, data_, function (resp) {
                        if (resp.state == 1) {
                            form.val('example', resp.data);
                            console.info(resp.data);
                            $("#jue").val(resp.data.roleId);
                        }
                    }, "json");
                }

                form.render();
            }
        },"json")
        //监听提交
        form.on('submit(demo1)', function (data) {
            let data_ = data.field;
            let url = "/logisticsUserRole/addAndUpdate";
            $.post(url, data_, function (resp) {
                if (resp.state == 1) {
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                    parent.table.reload('dataLXList');
                } else {
                    layer.msg(resp.mgs);
                }

            }, "json");
            return false;
        });

        /**
         * 得到地址上得值
         * @param id
         * @returns {string|null}
         */
        function getQueryString(id) {
            var reg = new RegExp("(^|&)" + id + "=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return unescape(r[2]);
            return null;
            console.info(r);
        }

        $(function () {
            $("#close_btn").on("click", function () {
                var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                parent.layer.close(index);
            });
        });



    });
</script>
</body>
</html>